/**
 * Created by caelan on 2018/10/20.
 */
/**
 * Created by caelan on 2018/10/20.
 */

var historyStatic = {
  html: function (staticArray) {
	if(staticArray==null||staticArray.length==0){
		return "";
	}
    var name = {
        HistoryNccStatistic: '常年值',
        HistoryNccDifStatistic: '距平',
        HistoryMaxStatistic: '历史最大值',
        HistoryMinStatistic: '历史最小值',
        HistoryOrderStatistic: '历史排位',
        HistoryAvgStatistic: '历史均值',
        HistoryRegSlopeStatistic: '历史变化趋势',
        HistoryAvgDifStatistic: '历史均值之差',
        HistorySecondMaxStatistic: '历史次大值',
        HistoryMaxDifStatistic: '与历史最大值之差'
      },
      html = '<div class="pluginsDiv" id="historyPluginsDiv">';

    var options = '<option value="">请选择统计类型</option>';
    for ( var i = 0, len = staticArray.length; i < len; i++ ) {
      if(Global.currentElement.indexOf("蒸散量")>-1){//当选中 “蒸散量” 时，下拉选框不做默认选中！“距平”和“常年值”
           options += '<option value="'+ staticArray[i] +'">'+ name[staticArray[i]] +'</option>'
      }else{//其他元素 均默认选中“距平”和“常年值”进行展示
          if(staticArray[i] == "HistoryNccStatistic" || staticArray[i] == "HistoryNccDifStatistic"){
              options += '<option selected value="'+ staticArray[i] +'">'+ name[staticArray[i]] +'</option>'
          }else{
              options += '<option value="'+ staticArray[i] +'">'+ name[staticArray[i]] +'</option>'
          }
          //  options += '<option value="'+ staticArray[i] +'">'+ name[staticArray[i]] +'</option>'
      }

    }

    html += '<div class="layui-form-item pluginsSingleRows">' +
      '<label class="layui-form-label">历史统计:</label>' +
      '<div class="layui-input-block">' +
        '<select data-selected="" lay-filter="historyMulti" multiple lay-omit lay-tools id="historyStaticSelect">'+ options +'</select>' +
      '</div>' +
    '</div>';

    html += '<div style="display: none" class="layui-form-item history-date pluginsSingleRows">' +
      '<label class="layui-form-label">历史时段:</label>' +
      '<div class="layui-input-block">' +
        '<input class="layui-input" id="historyDate" type="text">' +
      '</div>' +
    '</div>';

    html += '<div style="display: none" class="layui-form-item normal-date pluginsSingleRows">' +
      '<label class="layui-form-label">常年时段:</label>' +
      '<div class="layui-input-block">' +
        '<input class="layui-input" id="normalDate" type="text">' +
      '</div>' +
    '</div>';



    html += '</div>';
    return html;
  },
  getData: function () {
    var val = $('#historyStaticSelect').attr('data-selected') || '';
    Global.postData.statisticWays = val ? val.split(',') : [];

    /*常年和历史时间段*/
    var normalType = ['HistoryNccStatistic', 'HistoryNccDifStatistic'];
    for ( var i = 0, len = normalType.length; i < len; i++ ){
      if ( val.split(',').indexOf(normalType[i]) > -1 ) {
        var date = $('#normalDate').val().split('-');
        Global.postData.standStartYear = date[0].trim();
        Global.postData.standEndYear = date[1].trim();
        break;
      }
    }
    var historyType = ['HistoryMaxStatistic', 'HistoryMinStatistic', 'HistoryOrderStatistic', 'HistoryAvgStatistic', 'HistoryAvgDifStatistic', 'HistoryRegSlopeStatistic','HistorySecondMaxStatistic','HistoryMaxDifStatistic'];
    for ( var j = 0, lens = historyType.length; j < lens; j++ ){
      if ( val.split(',').indexOf(historyType[j]) > -1 ) {
        var dates = $('#historyDate').val().split('-');
        Global.postData.minYear = dates[0].trim();
        Global.postData.maxYear = dates[1].trim();
        break;
      }
    }

  },
  renderCallback: function () {//初始化执行的方法
    layForm.on('select(historyMulti)',historyStatic.onHistioryMultiChanged );
    if(Global.currentElement.indexOf("蒸散量")>-1){//蒸散量 初始化不绑定 距平和 常年值

    }else{
         historyStatic.onHistioryMultiChanged({elem:$("#historyStaticSelect"),value:['HistoryNccStatistic', 'HistoryNccDifStatistic'],current_value:'HistoryNccDifStatistic'});
    }
  },

  
  onHistioryMultiChanged : function (data) {
    console.log(data.elem); //得到select原始DOM对象
    console.log(data.value); //得到被选中的值（数组类型）
    console.log(data.othis); //得到美化后的DOM对象
    console.log(data.current_value); //当前操作的值（选中或取消的值）

    $('#historyStaticSelect').attr('data-selected', data.value.join(','));

    /*显示历史时间（常年和历史）*/
    var his = false,
        nor = false,
        historyType = ['HistoryMaxStatistic', 'HistoryMinStatistic', 'HistoryOrderStatistic', 'HistoryAvgStatistic', 'HistoryAvgDifStatistic', 'HistoryRegSlopeStatistic'],
        normalType = ['HistoryNccStatistic', 'HistoryNccDifStatistic'];
    for ( var i = 0, len = normalType.length; i < len; i++ ){
      if ( data.value.join(',').indexOf(normalType[i]) > -1 ) {
        nor = true;
        $('#historyPluginsDiv').find('.normal-date').css('display', 'block');
        break;
      }
    }
    for ( var j = 0, lens = historyType.length; j < lens; j++ ){
      if ( data.value.join(',').indexOf(historyType[j]) > -1 ) {
        his = true;
        $('#historyPluginsDiv').find('.history-date').css('display', 'block');
        break;
      }
    }
    if ( his === false )
      $('#historyPluginsDiv').find('.history-date').css('display', 'none');
      else $('#historyPluginsDiv').find('.history-date').css('display', 'block');
    if( nor === false )
      $('#historyPluginsDiv').find('.normal-date').css('display', 'none');
      else $('#historyPluginsDiv').find('.normal-date').css('display', 'block');
  }

};